﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Pavilion.DesignerTool
{
    public class BrushTool : IDrawTool
    {
        public Color BrushColor { get; set; }
        private Texture2D brushTexture;

        public BrushTool()
        {
            Size = 10;
            BrushColor = Color.Red;

            RenderBrushTexture();
        }

        private void RenderBrushTexture()
        {
            Render render = new Render();

            render.Begin(20, 20);
            DrawManager.Instance.FillRectangleOnScreen(new Rectangle(0, 0, 20, 20), new Color(255, 255, 255, 255));
            render.End();

            brushTexture = render.Texture;
        }

        public void Draw(System.Windows.Point point, float pressurePercentage)
        {
            Vector2 origin;

            if (Size > 1)
                origin = new Vector2(brushTexture.Width / 2);
            else
                origin = new Vector2(1);

            float scale = CalculateBrushScale((int)Size, brushTexture.Width, pressurePercentage);

            for (float i = 0f; i <= 16f; i += 0.1f)
                DrawManager.Instance.DrawOnScreen(brushTexture, new Vector2((float)point.X, (float)point.Y), new Rectangle(0, 0, brushTexture.Width, brushTexture.Height),
                    BrushColor, i, origin, scale, SpriteEffects.None, 0f);
        }

        public string Name { get { return this.GetType().Name; } }

        public float Size { get; set; }

        public int Opacity
        {
            get { return BrushColor.A; }
            set
            {
                if (value < 0 || value > 255)
                    throw new Exception();

                BrushColor = new Color(BrushColor.R, BrushColor.G, BrushColor.B, value);
            }
        }

        private float CalculateBrushScale(int size, int width, float pressurePercentage) { return ((size * pressurePercentage) / (float)width); }
    }
}
